﻿/*!
 *  @brief      创建UdpSocket线程（创建线程的一）- 单播
 *  @details    单播模式（unicast
 *  @author     YZG
 *  @version    1.0
 *  @date       2019-05-22
 *  @warning
 *  @copyright
 *  @note      一个UDP客户端发送数据报到指定地址和端口的另一UDP客户端，是一对一的数据传输。
 */
#ifndef UNICASTUDPSOCKETTASK_H
#define UNICASTUDPSOCKETTASK_H

#include "common_global.h"

#include <QObject>
#include <QAbstractSocket>
#include <QUdpSocket>

namespace Common {

	class COMMONSHARED_EXPORT UdpUnicast : public QObject
	{
		Q_OBJECT
	public:
		explicit UdpUnicast(QObject *parent = Q_NULLPTR);
		~UdpUnicast();

	public:
		/**
		 * @brief getUdpSocket 获取Udp通讯指针
		 * @return
		 */
		QUdpSocket * getUdpSocket();

		/**
		 * @brief createUnicast 创建UDP单播
		 * @param ip 本地IP
		 * @param port 本地端口
		 * @return
		 */
		bool createUnicast(QString ip, int port);

		/**
		 * @brief	createUnicast 创建UDP单播
		 * @param	port 本地端口 
		 * @return 
		 */
		bool createUnicast(int port);

		/**
		 * @brief closeUnicast 关闭UDP
		 * @return
		 */
		bool closeUnicast();

		/**
		 * @brief	sendToUnicastMsg 发生消息
		 * @param	targetIp 目标Ip
		 * @param	targetPort 目标端口
		 * @param	buff 发送的数据
		 * @return
		 */
		bool sendToUnicastMsg(QString targetIp, int targetPort, QByteArray buff);

	Q_SIGNALS:
		/**
		 * @brief signalRecvUnicastMsg 触发接收的消息
		 * @param sourceIp
		 * @param sourcePort
		 * @param recvMsg
		 */
		void signalRecvUnicastMsg(QString sourceIp, int sourcePort, QByteArray recvMsg);

	public Q_SLOTS:
		/**
		 * @brief slotUnicastReadyRead 读取数据
		 */
		void slotUnicastReadyRead();

		/**
		 * @brief slotSocketStateChange 网络状态信息改变
		 * @param state
		 */
		void slotSocketStateChange(QAbstractSocket::SocketState state);

	private:
		QUdpSocket *m_pUdpSocket;           // 单播模式
		bool m_bBind;                       // 是否绑定成功
	};

} //namespace Core

#endif // UNICASTUDPSOCKETTHREAD_H
